/*
 ============================================================================
 Name		: SchedulerTimer.cpp
 Author	  : PSL
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CSchedulerTimer implementation
 ============================================================================
 */

#include "SchedulerTimer.h"
#include "AutoTesterEngine.h"
#include "mtalog.h"

CSchedulerTimer::CSchedulerTimer() : CActive(EPriorityStandard), iHours(0), iPeriodCount(1) // Standard priority
{
}

CSchedulerTimer* CSchedulerTimer::NewLC()
{
	CSchedulerTimer* self = new ( ELeave ) CSchedulerTimer();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CSchedulerTimer* CSchedulerTimer::NewL()
{
	CSchedulerTimer* self = CSchedulerTimer::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CSchedulerTimer::ConstructL()
{
	User::LeaveIfError(iTimer.CreateLocal()); // Initialize timer
	CActiveScheduler::Add(this); // Add to scheduler
}

CSchedulerTimer::~CSchedulerTimer()
{
	Cancel(); // Cancel any request, if outstanding
	iTimer.Close(); // Destroy the RTimer object
	// Delete instance variables if any
}

void CSchedulerTimer::DoCancel()
{
	iTimer.Cancel();
}

void CSchedulerTimer::StartTimer(TUint aHours)
{
	iHours = aHours;
	
	Cancel(); // Cancel any request, just to be sure
	iTimer.After(iStatus, 600000000); // Set for later
	SetActive(); // Tell scheduler a request is active
}

void CSchedulerTimer::RunL()
{
	/* RunL would be called ONCE every TEN minutes; 6X1 would be ONE HOUR, 
	 * so iPeriodic = 6 implies one hour has past */
	if(iPeriodCount == (6 * iHours))
	{
		CAutoTester* autoTester = CAutoTester::GetInstance();
		autoTester->Replay();
		iPeriodCount = 0;
	}
	else
	{
		iPeriodCount++;
		iTimer.After(iStatus, 600000000); // Set for later
		SetActive(); // Tell scheduler a request is active
	}
}

TInt CSchedulerTimer::RunError(TInt aError)
{
	return aError;
}
